<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="Controlador.ConnectionDB"%>
<%@page import="Modelo.Querys"%>
<%@page import="java.sql.ResultSet"%>
<%@page pageEncoding="UTF-8" %>
<%
    String Accion = request.getParameter("Type");

    if (Accion != null) {
        if (Accion.equals("1")) {
            String Action = request.getParameter("Action");
            ConnectionDB c = new ConnectionDB();
            if (c.Connect()) {                
                out.println("<select id=\"basic\" name=\"basic\" multiple=\"multiple\">");
                ResultSet rs = c.getQuery("select b.name, b.code from actionincident a, incident b where a.incident = b.code and a.action ='"+Action+"'");
                try {
                    List<String> l = new ArrayList<String>();
                    while (rs.next()) {
                        l.add(rs.getString(2));
                        out.println("<option value=\"" + rs.getString(2) + "\" selected=\"selected\">" + rs.getString(1) + "</option>");
                    }
                    String query = "select name, code from incident ";
                    if (l.size() > 0) {                        
                        for (int i = 0; i < l.size(); i++) {
                            if (i == 0) {
                                query += "where code <> '" + l.get(i)+ "'\n";
                            } else {
                                query += " and code <> '"+l.get(i)+"'";
                            }
                        }
                    }
                    rs = c.getQuery(query);
                    while(rs.next()){
                        out.println("<option value=\""+rs.getString(2) +"\">"+rs.getString(1) +"</option>");
                    }
                    out.println("</select>");
                } catch (Exception e) {
                }
                c.close();
            } 
%>
<script type="text/javascript">
    $(function(){
        $("#basic").pickList();
    });    
</script>
<%            // fin de Type = 1
        }else if(Accion.equals("2")) {
            String data = request.getParameter("Datos");
            String Action = request.getParameter("Action");
             String msj = "Asignaciones Realizadas con Exito";
            ConnectionDB c = new ConnectionDB();
            if(c.Connect()){
                if(data.isEmpty()){
                    c.getUpdate("delete from actionincident where action = '"+Action+"'");
                    c.commit(true);
                }else{
                    String[] v = data.split(";");                      
                    c.getUpdate("delete from actionincident where action = '"+Action+"'");                    
                    for(int i = 0; i<v.length; i++){                        
                        if(v[i].length() == 1){
                            v[i] = "0000"+v[i];
                        }else if(v[i].length() == 2){
                            v[i] = "000"+v[i];
                        }else if(v[i].length() == 3){
                            v[i] = "00"+v[i];
                        }else if(v[i].length() == 4){
                            v[i] = "0"+v[i];
                        }
                        
                        c.getUpdate("insert into actionincident (action, incident) values('"+Action+"','"+v[i]+"');");
                    }  
                    c.commit(true);
                    c.close();
                }
            }else{
                msj = "No se pudo conectar a la base de datos :(";
            }
%>
<script type="text/javascript">
    var ActionSelected = $("#SelectAction option:selected").val();
    $("#AsigSelect").load("Contr_aai.jsp?Type=1&Action="+ActionSelected);
</script>
<%
            out.print(msj);
            
        }
    }

%>